有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我需要帮助在for循环中使用大写这个词,这个词不是用eclipse编写的,而是从txt文件导入的

import java.util.*;
import java.io.*;

public class Cascader {
    public static void main(String args[]) throws IOException {
        Scanner inputfile = new Scanner(new File("name.txt"));
        String name = inputfile.nextLine();
        System.out.println("Reading the name...");
        for(int i = 0; i < name.length(); i++) {
            System.out.println(name.charAt(i));
        }
        inputfile.close();
    }
}

共 (1) 个答案

  1. # 1 楼答案

    回答您的问题:您可能没有“保存”新字符串。toUpperCase()方法返回一个新字符串,其中每个字符都已改为大写

    name.toUpperCase(); // does not do anything to the String in the variable 'name'
    String newName = name.toUpperCase(); // if name was 'john', newName will be 'JOHN'
    name = name.toUpperCase(); // if name was 'john', name will now be 'JOHN'